package ntut.andengine.gamedesign;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.opengl.buffer.BufferObjectManager;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.sensor.accelerometer.AccelerometerData;
import org.anddev.andengine.sensor.accelerometer.IAccelerometerListener;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

public class MainGameActivity extends BaseGameActivity implements IAccelerometerListener {
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 800;

	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;
	private GameSceneLogo mGameSceneLogo;
	private GameSceneLevel mGameSceneLevel;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public void setGameSceneLevel(GameSceneLevel gameSceneLevel){
		mGameSceneLevel = gameSceneLevel;
	}
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		this.mCamera.setZClippingPlanes(-100, 100);
		final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
		engineOptions.getTouchOptions().setRunOnUpdateThread(true);
		return new Engine(engineOptions);
	}

	@Override
	public void onLoadResources() {
		BitmapTextureRegionFactory.setAssetBasePath("gfx/");
		
		mGameSceneLogo = new GameSceneLogo(this, mEngine);
		mGameSceneLogo.onLoadResources();
		
		Log.d("ntut","hello");
	}

	@Override
	public Scene onLoadScene() {
		final Scene scene = mGameSceneLogo.onLoadScene();
		scene.setBackground(new ColorBackground(0, 0, 0));
		
		/* UPDATE */	 
		this.mEngine.registerUpdateHandler(new IUpdateHandler(){			
			
			@Override
			public void onUpdate(float pSecondsElapsed) {
				
			}

			@Override
			public void reset() {
				
			}});
		
		Log.d("ntut","onLoadScene");

		return scene;
	}

	@Override
	public void onLoadComplete() {

	}
	
	@Override
	public void onResumeGame() {
		super.onResumeGame();

		this.enableAccelerometerSensor(this);
	}
	
	@Override
	public void onPauseGame() {
		super.onPauseGame();

		this.disableAccelerometerSensor();
	}

	@Override
	public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
		if(mGameSceneLevel != null)
			mGameSceneLevel.onAccelerometerChanged(pAccelerometerData);
	}

	// ===========================================================
	// Methods
	// ===========================================================

	static void startActivity(Activity activity, Class<?> nextActivity){
		BufferObjectManager.getActiveInstance().clear();
		activity.finish();
		activity.startActivity(new Intent(activity, nextActivity));
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================

}
